package ${cfg.basePackage}.module.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @date ${date}
 */
@Slf4j
@Service
public class ${entity}SerImpl implements ${entity}Ser {
    @Autowired
    private ${entity}Dao ${table.entityPath}Dao;

    //添加一条数据
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R insert(${entity} param) {
        ${entity} ${table.entityPath} = new ${entity}();

        //        检查属性重复
        ${table.entityPath}.setName(param.getName());
        if (${table.entityPath}Dao.isHaveByPo(${table.entityPath}, DAO.eq)) {
        R.byError(ResMsg.ERROR_NAME);
        }

        //添加数据
        ${table.entityPath} = new ${entity}();
        ${table.entityPath}.setId(nextId());
        ${table.entityPath}.setName(param.getName());
        ${table.entityPath}.setLogo(param.getLogo());
        ${table.entityPath}.setSort(param.getSort());
        ${table.entityPath}.setFileIds(param.getFileIds());
        ${table.entityPath}.setTypeIds(param.getTypeIds());
        ${table.entityPath}Dao.insert(${table.entityPath});

        return R.bySuccess();
    }

    //删除数据
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R delete(Long[] ids) {
    //    for (int i = 0; i < ids.length; i++) {
    //        BaseWrapper<${entity}> wrapper = new BaseWrapper<>();
    //        wrapper.eq(${entity}.ID, ids[i]);
    //        ${table.entityPath}Dao.delete(wrapper);
    //    }
    ${table.entityPath}Dao.deleteBatchIds(Arrays.asList(ids));
        return R.bySuccess();
    }

    //    修改一条数据
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R update(${entity} param) {
        //添加/修改 数据
        ${entity} ${table.entityPath} = new ${entity}();
        ${table.entityPath}.setId(param.getId());
        ${table.entityPath}.setName(param.getName());
        ${table.entityPath}.setLogo(param.getLogo());
        ${table.entityPath}.setSort(param.getSort());
        ${table.entityPath}.setFileIds(param.getFileIds());
        ${table.entityPath}.setTypeIds(param.getTypeIds());
        ${table.entityPath}Dao.updateById(${table.entityPath});
        return R.bySuccess();
    }

    //    查询数据集合
    @Override
    public R list(${entity} param) {
        return R.bySuccess(${table.entityPath}Dao.listByPo(param, DAO.eq));
    }

    //    分页查询数据
    @Override
    public R page(R param) {
        Wrapper<${entity}> wrapper = new Wrapper<>();
        wrapper.orderByAsc(${entity}.SORT,${entity}.CREATE_TIME);
        like(${entity}.ID, param.get("id"), wrapper);
        like(${entity}.NAME, param.get("name"), wrapper);
        //        if (ValidateKit.isNotBlank(param.get("time")) && param.getJSONArray("time").size() == 2) {
        //            wrapper.between(${entity}.CREATE_TIME, param.getJSONArray("time").get(0), param.getJSONArray("time").get(1));
        //        }
        IPage<${entity}> ${table.entityPath}IPage = ${table.entityPath}Dao.selectPage(new BasePage<>(param), wrapper);
        return R.bySuccess(${table.entityPath}IPage);
    }

    //查询一条数据
    @Override
    public R one(R param) {
        ${entity} ${table.entityPath} = ${table.entityPath}Dao.selectById(param.getLong("id"));
        //        Wrapper<${entity}> wrapper = new Wrapper<>();
        //        eq(${entity}.NAME, param.get("name"), wrapper);
        //        ${entity} ${table.entityPath}2 = ${table.entityPath}Dao.selectOne(wrapper);
        return R.bySuccess(${table.entityPath});
    }
}